<?php
  defined('_JEXEC') or die();
  jimport('joomla.application.component.model');
  require_once('base.php');
  
  class PlayerGameStatModelPlayerGameStat extends BaseModel
  {     
    var $_name = "playergamestat";
    var $_table = "#__pbny_player_game_stats";
    
    function get_list_summary($id = null){
      
      $t_schedule = '#__pbny_schedules';
      $t_players  = '#__pbny_players';
      $t_persons  = '#__pbny_persons';
      
      $sql = 'SELECT pgs.*,
          concat(p.lastname, \', \', p.firstname) as name,
          s.schedule ' .
           'FROM ' . $this->_table . ' pgs ' .
           "LEFT JOIN $t_schedule s ON s.id=pgs.schedule_id ".
           "LEFT JOIN $t_players ps ON ps.id=pgs.player_id ".
           "LEFT JOIN $t_persons p ON p.id=ps.person_id ";
      
      $where = null;
      if($id)
        $where = ' WHERE id = ' . $id;
      
      $this->_sql = $sql;
          
      return $this->getListData();
    }
    
    function get_by_schedule_id($schedule_id, $team_id = 0){
      
      $sql = 'SELECT pgs.* '.
             'FROM '. $this->_table.' as pgs';
      $where = " WHERE pgs.schedule_id=$schedule_id ";
           
      if($team_id){
        $t_players = '#__pbny_players';
        $sql .= " LEFT JOIN $t_players ps ON pgs.player_id = ps.id";
        $where .= " AND ps.team_id = $team_id";
        }
      $sql .= $where;
      return $this->_get($sql);
    }
    
    function get_avg_season_stats($player_id, $season_id){
    
      $result = array();
      $stats = $this->get_season_stats_by_player_id($player_id, $season_id);
      if(!$stats)
        return $result;
        
      $result['points']        = 0;
      $result['field_goals']      = 0;
      $result['field_goal_attempts']  = 0;
      $result['three_points']      = 0;
      $result['three_point_attempts']  = 0;
      $result['free_throws']      = 0;
      $result['free_throw_attempts']  = 0;
      $result['off_rebounds']      = 0;
      $result['def_rebounds']      = 0;
      $result['assists']        = 0;
      $result['steals']        = 0;
      $result['blocks']        = 0;
      
      foreach($stats as $stat){
        
        $result['points']         += $stat->points;
        $result['field_goals']         += $stat->field_goals;
        $result['field_goal_attempts']    += $stat->field_goal_attempts;
        $result['three_points']       += $stat->three_points;
        $result['three_point_attempts']   += $stat->three_point_attempts;
        $result['free_throws']         += $stat->free_throws;
        $result['free_throw_attempts']    += $stat->free_throw_attempts;
        $result['off_rebounds']        += $stat->off_rebounds;
        $result['def_rebounds']        += $stat->def_rebounds;
        $result['assists']          += $stat->assists;
        $result['steals']          += $stat->steals;
        $result['blocks']         += $stat->blocks;
      }
      
      $games      = count($stats);
      foreach($result as $key=>$value)
        $result["avg_$key"] = number_format($value/$games,2);
      
      $result['games_played'] = $games;
      return $result;
    }
    
    function get_season_stats_by_player_id($player_id, $season_id){
    
      $sql = 'SELECT pgs.* '.
           "FROM $this->_table pgs ".
           'LEFT JOIN #__pbny_schedules s ON s.id = pgs.schedule_id '.
           'LEFT JOIN #__pbny_division_conferences dc on dc.conference_id = s.conference_id '.
           'LEFT JOIN #__pbny_season_divisions sd on sd.division_id = dc.division_id '.
           'LEFT JOIN #__pbny_seasons ss on ss.id = sd.season_id '.
           'WHERE s.schedule>=ss.start_date '.
           ' AND s.schedule<=ss.end_date'. 
           " AND sd.season_id=$season_id ".
           " AND pgs.player_id=$player_id ";
           
      return $this->_get($sql);
    }

    function get_by($schedule_id = null, $player_id = null){

      $conditions = array();
      if(!empty($schedule_id))
        array_push($conditions, "schedule_id=$schedule_id");
      if(!empty($player_id))
        array_push($conditions, "player_id=$player_id");

      $sql = 'SELECT pgs.* '.
           "FROM $this->_table pgs ".
           "WHERE ".implode(' AND ', $conditions);
      return $this->_get($sql, true);

    }
  
  }
?>
